<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>shell 练习-监控磁盘使用率 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>shell 练习-监控磁盘使用率</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>写一个shell脚本，检测所有磁盘分区使用率和inode使用率并记录到以当天日期为命名的日志文件里，当发现某个分区容量或者inode使用量大于85%时，发邮件通知你自己。</p>
<p>思路：就是先df -h 然后过滤出已使用的那一列，然后再想办法过滤出百分比的整数部分，然后和85去比较，同理，inode也是一样的思路。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment">## This script is for record Filesystem Use%,IUse% everyday and send alert mail when % is more than 85%.</span></div><div class="line"></div><div class="line"><span class="built_in">log</span>=/var/<span class="built_in">log</span>/disk/`date +%F`.<span class="built_in">log</span></div><div class="line">date +<span class="string">'%F %T'</span> &gt; <span class="variable">$log</span></div><div class="line">df -h &gt;&gt; <span class="variable">$log</span></div><div class="line"><span class="built_in">echo</span> &gt;&gt; <span class="variable">$log</span></div><div class="line">df -i &gt;&gt; <span class="variable">$log</span></div><div class="line"></div><div class="line"><span class="keyword">for</span> i <span class="keyword">in</span> `df -h|grep -v <span class="string">'Use%'</span>|sed <span class="string">'s/%//'</span>|awk <span class="string">'&#123;print $5&#125;'</span>`; <span class="keyword">do</span></div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$i</span> -gt 85 ]; <span class="keyword">then</span></div><div class="line">        use=`df -h|grep -v <span class="string">'Use%'</span>|sed <span class="string">'s/%//'</span>|awk <span class="string">'$5=='</span><span class="variable">$i</span><span class="string">' &#123;print $1,$5&#125;'</span>`</div><div class="line">        <span class="built_in">echo</span> <span class="string">"<span class="variable">$use</span>"</span> &gt;&gt; use</div><div class="line">    <span class="keyword">fi</span></div><div class="line"><span class="keyword">done</span></div><div class="line"><span class="keyword">if</span> [ -e use ]; <span class="keyword">then</span></div><div class="line">   <span class="comment">##这里可以使用咱们之前介绍的mail.py发邮件</span></div><div class="line">    mail -s <span class="string">"Filesystem Use% check"</span> root@localhost &lt; use</div><div class="line">    rm -rf use</div><div class="line"><span class="keyword">fi</span></div><div class="line"></div><div class="line"><span class="keyword">for</span> j <span class="keyword">in</span> `df -i|grep -v <span class="string">'IUse%'</span>|sed <span class="string">'s/%//'</span>|awk <span class="string">'&#123;print $5&#125;'</span>`; <span class="keyword">do</span></div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$j</span> -gt 85 ]; <span class="keyword">then</span></div><div class="line">        iuse=`df -i|grep -v <span class="string">'IUse%'</span>|sed <span class="string">'s/%//'</span>|awk <span class="string">'$5=='</span><span class="variable">$j</span><span class="string">' &#123;print $1,$5&#125;'</span>`</div><div class="line">        <span class="built_in">echo</span> <span class="string">"<span class="variable">$iuse</span>"</span> &gt;&gt; iuse</div><div class="line">    <span class="keyword">fi</span></div><div class="line"><span class="keyword">done</span></div><div class="line"><span class="keyword">if</span> [ -e iuse ]; <span class="keyword">then</span></div><div class="line">    mail -s <span class="string">"Filesystem IUse% check"</span> root@localhost &lt; iuse</div><div class="line">    rm -rf iuse</div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
<p>思路：</p>
<ol>
<li>df -h、df -i 记录磁盘分区使用率和inode使用率，date +%F 日志名格式</li>
<li>取出使用率(第5列)百分比序列，for循环逐一与85比较，大于85则记录到新文件里，当for循环结束后，汇总超过85的一并发送邮件(邮箱服务因未搭建，发送本地root账户)。</li>
</ol>
<p>此脚本正确运行前提：该系统没有逻辑卷的情况下使用，因为逻辑卷df -h、df -i 时，使用率百分比是在第4列，而不是第5列。如有逻辑卷，则会漏统计逻辑卷使用情况。</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/Shell 练习/24. shell 练习-监控磁盘使用率/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
